package com.vitaf.cash.app.utils.helper

import com.vitaf.cash.app.VitafCashApplication
import com.vitaf.cash.app.configs.GombeCreditApiUrl.addCommonParams
import com.vitaf.cash.app.event.VitafEventData
import com.vitaf.cash.app.utils.shared.VitafSharedHp
import com.vitaf.cash.app.utils.shared.VitafSharedKey
import com.vitaf.cash.app.configs.GombeCreditApiPath
import com.vitaf.cash.app.configs.GombeCreditApiUrl
import com.vitaf.cash.app.request.HttpHelper
import com.vitaf.cash.app.request.IHttpCallback
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/20 18:06
 * @Annotation：保存自定义数据接口 参数
 */
object VitafCustomDataHp {

    /**
     * userName                         : 用户名
     * userAvatarUrl                    ： 用户头像链接
     */

    /**
     * 获取保存的自定义数据
     */
    fun takeGombeCreditCustomData(): JSONObject {
        val customData = VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_CUSTOM_DATA)
        if (customData.isEmpty()){
            return JSONObject()
        }
        return JSONObject(customData)
    }

    /**
     * 获取用户昵称
     */
    fun takeGombeCreditCustomUserName(): String{
        val data = takeGombeCreditCustomData()
        return data.optString("userName","")
    }

    /**
     * 获取用户头像地址
     */
    fun takeGombeCreditCustomUserAvatarUrl(): String{
        val data = takeGombeCreditCustomData()
        return data.optString("userAvatarUrl","")
    }

    /**
     * 保存用户昵称
     */
    fun saveGombeCreditCustomUserNameAction(userName: String){
        val obj = takeGombeCreditCustomData()
        obj.put("userName",userName)
        saveGombeCreditCustomDataAction(obj)
    }

    /**
     * 保存用户头像id
     */
    fun saveGombeCreditCustomUserAvatar(imgUrl: String){
        val obj = takeGombeCreditCustomData()
        obj.put("userAvatarUrl",imgUrl)
        saveGombeCreditCustomDataAction(obj)
    }

    /**
     * 获取自定义数据
     */
    fun takeGombeCreditCustomDataAction(){
        val httpUrl = GombeCreditApiUrl.takeRequestUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", GombeCreditApiPath.GOMBECREDIT_PATH.GOMBECREDIT_API_CUSTOM_PARAMS_PATH)
        }
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null && obj.has("userInfo")){
                            val userInfo = obj.optString("userInfo")
                            //保存自定义数据
                            VitafSharedHp.addGombeCreditSharedData(VitafSharedKey.GOMBECREDIT_CUSTOM_DATA,userInfo.toString())
                        }
                        //通知自定义数据刷新完成
                        VitafCashApplication.app.publisher.publish(
                            VitafEventData(
                                VitafEventData.TYPE.GOMBECREDIT_CUSTOM_DATA_REFRESHED
                            )
                        )
                    }
                    else -> {
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 保存数据
     */
    private fun saveGombeCreditCustomDataAction(obj: JSONObject, callback: IHttpCallback? = null){
        val httpUrl = GombeCreditApiUrl.takeRequestUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", GombeCreditApiPath.GOMBECREDIT_PATH.GOMBECREDIT_API_SAVE_CUSTOM_PARAMS_PATH)
            put("userInfo",obj.toString())
        }
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback?.onRequestSuccess(data)
                when(data.optInt("code")) {
                    0 -> {//成功
                        val jsonObj = data.optJSONObject("obj")
                        if (jsonObj != null){
                            //保存成功 再更新数据
                            takeGombeCreditCustomDataAction()
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback?.onRequestError(error)
            }

            override fun onRequestFinish() {
                callback?.onRequestFinish()
            }
        })
    }
}